package com.example.plat.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.plat.enity.Activity;
import com.example.plat.enity.Comment;
import com.example.plat.enity.Place;
import com.example.plat.enity.dto.ActivityCardDto;
import com.example.plat.enity.dto.ActivityCreateDto;
import com.example.plat.enity.dto.ActivityDetailDto;
import com.example.plat.enity.dto.CommentShowDto;
import com.example.plat.exception.ActivityException;
import com.example.plat.service.GetActivityService;
import com.example.plat.service.PlaceService;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.modelmapper.ModelMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/home")
public class GetActivityController {

    @Autowired
    ModelMapper modelMapper;

    @Autowired
    private GetActivityService service;

    @Autowired
    private PlaceService placeService;

    @ApiOperation("根据活动ID查询活动详情")
    @GetMapping("/{actId}")
    public ResponseEntity<ActivityDetailDto> lookActivity(@PathVariable Long actId, Long userId) throws ActivityException {
        Activity activity = service.lookActivity(actId);
        Place place = service.getPlace(activity.getPlaceId());
        List<String> imgList = service.getImage(actId);

        ActivityDetailDto dto = modelMapper.map(activity, ActivityDetailDto.class);
        dto.setStar(service.isJoin(userId,actId));
        dto.setImgList(imgList);
        dto.setPlace(place);

        return ResponseEntity.ok(dto);
    }

    @ApiOperation("根据活动ID查询创建活动信息")
    @GetMapping("/create/{actId}")
    public ResponseEntity<ActivityCreateDto> getCreateActivity(@PathVariable Long actId){
        Activity activity = service.getCreateActivity(actId);
        Place place = placeService.getPlace(activity.getPlaceId());

        ActivityCreateDto dto = modelMapper.map(activity, ActivityCreateDto.class);
        dto.setPlace(place);
        dto.setImgList(service.getImage(activity.getPlaceId()));

        return ResponseEntity.ok(dto);
    }

    @ApiOperation("根据各种条件分页查询活动卡片")
    @GetMapping
    public IPage<ActivityCardDto> findProduct(@ApiParam("标题")String title,
                                              @ApiParam("发布人")String publisher,
                                              @ApiParam("内容")String content,
                                              @ApiParam("时间")String time,
                                              @ApiParam("地点")String place,
                                              @ApiParam("审核状态")Boolean isPass,
                                              @ApiParam("分类")String category,
                                              @ApiParam("是否剩余")Boolean isSurplus,
                                              @RequestParam(defaultValue = "0")Integer pageNum,
                                              @RequestParam(defaultValue = "10")Integer pageSize){
        Map<String,Object> condition=new HashMap<>();
        if(title!=null) {
            condition.put("title","%"+title+"%");
        }
        if(publisher!=null) {
            condition.put("publisher",publisher);
        }
        if(content!=null) {
            condition.put("content","%"+content+"%");
        }
        if(time!=null) {
            condition.put("time",time);
        }
        if(place!=null) {
            condition.put("place","%"+place+"%");
        }
        if(isPass!=null) {
            condition.put("isPass",isPass);
        }
        if(category!=null) {
            condition.put("category",category);
        }
        if(isSurplus!=null) {
            condition.put("isSurplus",isSurplus);
        }
        IPage<Activity> page = service.findActivityByConditions(condition, pageNum, pageSize);
        return page.convert(u -> {
            ActivityCardDto v = new ActivityCardDto();
            BeanUtils.copyProperties(u, v);
            return v;
        });
    }

    @ApiOperation("搜索")
    @GetMapping("/search")
    public IPage<ActivityCardDto> search(@RequestParam String condition,
                                         @RequestParam(defaultValue = "0")Integer pageNum,
                                         @RequestParam(defaultValue = "10")Integer pageSize){
        IPage<Activity> page = service.search("%" + condition + "%", pageNum, pageSize);
        return page.convert(u -> {
            ActivityCardDto v = new ActivityCardDto();
            BeanUtils.copyProperties(u, v);
            return v;
        });
    }
}
